package com.fulihui.weixinclient.model;

import java.util.HashMap;
import java.util.Map;

import org.near.toolkit.common.RandomCharset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fulihui.weixinclient.util.WeixinSign;

/**
 * WeixinJsapiSign 构造器
 * Created by willa on 2016/1/3.
 */
public class WeixinJsapiSignBuilder {
    private static final Logger log = LoggerFactory.getLogger(WeixinJsapiSignBuilder.class);

    public static WeixinJsapiSign build(String appId, String ticket, String url) {
        log.debug("WeixinJsapiSignBuilder.build - args[{}, {}, {}]", new Object[] { appId, ticket,
                url });
        WeixinJsapiSign weixinJsapiSign = new WeixinJsapiSign();
        weixinJsapiSign.appId = appId;
        weixinJsapiSign.timestamp = (int) (System.currentTimeMillis() / 1000);
        weixinJsapiSign.nonceStr = RandomCharset.randomMixture(16);

        Map<String, Object> param = new HashMap<>();
        param.put("noncestr", weixinJsapiSign.nonceStr);
        param.put("jsapi_ticket", ticket);
        param.put("timestamp", String.valueOf(weixinJsapiSign.timestamp));
        param.put("url", url);
        weixinJsapiSign.signature = WeixinSign.genJsapiSign(param);
        log.debug("weixinJsapiSign: {}", weixinJsapiSign);
        return weixinJsapiSign;
    }
}
